package org.example.gui.data;

import org.example.dao.BudgetDAO;
import org.example.dao.RecordDAO;
import org.example.entity.Record;
import org.example.util.DateUtil;

import java.text.DecimalFormat;
import java.util.List;

/**
 * SpendPanel的页面类 ，用来装载所有数据
 */
public class SpendData {
    public String monthSpend;
    public String todaySpend;
    public String avgSpendPerDay;
    public String monthAvailable;
    public String dayAvgAvailable;
    public String monthLeftDay;
    public int usagePercentageMonthly;     // 本月预算使用百分比
    public int usagePercentageDaily;       // 当日预算使用百分比
    public boolean isOverSpend = false;

    private SpendData(double monthSpend, double todaySpend, double avgSpendPerDay,
                      double monthAvailable, double dayAvgAvailable, int monthLeftDay,
                      int usagePercentageMonthly, int usagePercentageDaily) {
        DecimalFormat df = new DecimalFormat("0.00");
        this.monthSpend = "￥" + df.format(monthSpend);
        this.todaySpend = "￥" + df.format(todaySpend);
        this.avgSpendPerDay = "￥" + df.format(avgSpendPerDay);

        if (monthAvailable < 0)
            isOverSpend = true;
        if (!isOverSpend) {
            this.monthAvailable = "￥" + df.format(monthAvailable);
            this.dayAvgAvailable = "￥" + df.format(dayAvgAvailable);
        } else {
            this.monthAvailable = "超支￥" + df.format(-monthAvailable);
            this.dayAvgAvailable = "￥" + 0;
        }

        this.monthLeftDay = monthLeftDay + "天";
        this.usagePercentageMonthly = usagePercentageMonthly;
        this.usagePercentageDaily = usagePercentageDaily;
    }


    public static SpendData getSpendData() {
        List<Record> thisMonthRecords = RecordDAO.listThisMonth();    // 本月消费记录
        List<Record> todayRecords = RecordDAO.listToday();            // 今日消费记录
        int thisMonthTotalDay = DateUtil.thisMonthTotalDay();   // 本月总天数

        double monthSpend = 0;         // 本月消费
        double todaySpend = 0;         // 今日消费
        double avgSpendPerDay = 0;     // 日均消费（本月）
        double monthAvailable = 0;     // 本月剩余可用预算
        double dayAvgAvailable = 0;    // 日均可用预算
        int monthLeftDay = 0;       // 距离月末天数
        int usagePercentageMonthly = 0;     // 当月消费金额占预算百分比（0~100）
        int usagePercentageDaily = 0;       // 当日消费金额占预算百分比（0~100）


        int monthBudget = BudgetDAO.getIntBudget();   // 每月总预算

        for (Record record : thisMonthRecords) {
            monthSpend += record.getSpend();
        }


        for (Record record : todayRecords) {
            todaySpend += record.getSpend();
        }

        avgSpendPerDay = monthSpend / thisMonthTotalDay;
        monthAvailable = monthBudget - monthSpend;
        monthLeftDay = DateUtil.thisMonthLeftDay();
        dayAvgAvailable = monthAvailable / monthLeftDay;
        usagePercentageMonthly = (int) (monthSpend * 100 / monthBudget);
        usagePercentageDaily = (int) (todaySpend * 100 / (monthBudget / thisMonthTotalDay));


        return new SpendData(monthSpend, todaySpend, avgSpendPerDay,
                monthAvailable, dayAvgAvailable, monthLeftDay,
                usagePercentageMonthly, usagePercentageDaily);
    }
}

